from django import forms

from hot.forms.bootstrapform import BootStrapForm
from hot.utils import encry
from hot.models import Administrator





class LoginForm(BootStrapForm, forms.Form):
    no_widget_list = []
    """登录校验"""
    username = forms.CharField(
        max_length=255,
        required=False,
        widget=forms.TextInput,
        label='用户名',
    )
    password = forms.CharField(
        max_length=255,
        required=False,
        widget=forms.PasswordInput(render_value=True),
        label='密码',
    )

    def clean_password(self):
        password = self.cleaned_data['password']
        password = encry.md5_string(password)
        username = self.cleaned_data['username']
        obj = Administrator.objects.filter(username=username , password=password).first()
        if not obj:
            raise forms.ValidationError('用户名或密码错误')
        return password
